package io.binghe.im.openai.interfaces;

import io.binghe.im.openai.common.enums.HttpCode;
import io.binghe.im.openai.common.exception.OpenAIException;
import io.binghe.im.openai.common.response.ResponseMessage;
import io.binghe.im.openai.common.response.ResponseMessageFactory;
import io.binghe.im.openai.domain.service.OpenAIInteractiveService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

/**
 * @author binghe(微信 : hacker_binghe)
 * @version 1.0.0
 * @description 对外提供Http接口
 * @github https://github.com/binghe001
 * @copyright 公众号: 冰河技术
 */
@RestController
@RequestMapping("/openai")
public class OpenAIController {

    @Autowired
    private OpenAIInteractiveService openAIInteractiveService;

    @RequestMapping("/sendMessage")
    public ResponseMessage<String> sendMessage(String requestData){
        if (StringUtils.isEmpty(requestData)){
            throw new OpenAIException(HttpCode.PARAMS_ERROR);
        }
        try {
            return ResponseMessageFactory.getSuccessResponseMessage(openAIInteractiveService.sendMessage(requestData));
        } catch (IOException e) {
            throw new OpenAIException(HttpCode.PROGRAM_ERROR);
        }
    }
}
